package com.smile.chatview;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.smile.model.FriendInfo;

public class ChatMainView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private MePanel panel_me;
	private FriendsPanel panel_friends;
	private ChatPanel panel_chat;
	/**
	 * 鼠标点击时的坐标
	 */
	private int nowX;
	private int nowY;
	/**
	 * 窗口的坐标
	 */
	private int jfX;
	private int jfY;
	private SettingButton settingButton;
	private BasicButton chatButton;
	private BasicButton friendButton;
	private JTextField searchTextField;
	private SearchTextField stf;
	private HeadImage headImg;
	private JList<Object> friendsList;
	private JScrollPane scrollPane;
	private JLabel notConn;
	private JPanel panel_conn;
	private JButton minButton;
	private JButton exitButton;
	private JPanel topPanel;
	private JTextField isOnlineLabel;
	private JTextField friendName;
	private JScrollPane chat_scrollPane;
	private JPanel panel;
	private JScrollPane edit_scrollPane;
	private JTextArea textArea;

	private FriendInfo whichFriendsSelected = null;

	private DefaultListModel<Object> dlmFriends = new DefaultListModel<Object>();
	private DefaultListModel<Object> dlmChats = new DefaultListModel<Object>();
	private JLabel onlineImg;
	private JList<Object> chatMessageList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChatMainView frame = new ChatMainView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ChatMainView() {

		setUndecorated(true);
		setBackground(new Color(0, 0, 0, 0));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 820, 590);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		contentPane = new JPanel();
		contentPane.setOpaque(false);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		setMePanel();
		setFriendsPanel();
		setChat();

		panel_conn = new JPanel();
		panel_conn.setBackground(Color.RED);
		panel_conn.setBounds(300, 10, 250, 30);
		contentPane.add(panel_conn);
		panel_conn.setLayout(null);

		notConn = new JLabel("与服务器断开连接");
		notConn.setBounds(0, 0, 250, 30);
		notConn.setHorizontalAlignment(SwingConstants.CENTER);
		panel_conn.add(notConn);
		notConn.setFont(new Font("宋体", Font.BOLD, 16));
		notConn.setForeground(Color.WHITE);
		panel_conn.setVisible(false);

	}

	private void setMePanel() {
		panel_me = new MePanel();
		panel_me.setOpaque(false);
		panel_me.setBounds(0, 40, 80, 550);

		panel_me.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				setLocation(jfX + (e.getXOnScreen() - nowX),
						jfY + (e.getYOnScreen() - nowY));
			}
		});
		panel_me.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				nowX = e.getXOnScreen();
				nowY = e.getYOnScreen();
				jfX = getX();
				jfY = getY();
			}
		});

		contentPane.add(panel_me);
		panel_me.setLayout(null);

		String[] imgNames1 = { "icon1_b.png", "icon1_w.png" };
		chatButton = new BasicButton("", imgNames1);
		chatButton.setBounds(0, 110, 80, 50);
		panel_me.add(chatButton);
		chatButton.setClick(true);

		String[] imgNames2 = { "icon2_b.png", "icon2_w.png" };
		friendButton = new BasicButton("", imgNames2);
		friendButton.setBounds(0, 160, 80, 50);
		panel_me.add(friendButton);

		String[] imgNames3 = { "icon3_b.png", "icon3_w.png" };
		settingButton = new SettingButton("", imgNames3);
		settingButton.setBounds(28, 510, 24, 24);
		panel_me.add(settingButton);

		headImg = new HeadImage("dzq3.jpg");
		// headImg.setBounds(17, 17, 45, 45);
		panel_me.add(headImg);

		chatButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				friendButton.setClick(false);
			}
		});
		friendButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				chatButton.setClick(false);
			}
		});

	}

	private void getData() {
		dlmFriends.add(0, new FriendInfo("dzq1.jpg", "小熊"));
		dlmFriends.add(0, new FriendInfo("1.jpg", "234r23"));
		dlmFriends.add(0, new FriendInfo("dzq2.jpg", "sadfca"));
		dlmFriends.add(0, new FriendInfo("dzq3.jpg", "2请发送的2请发送的2请发送的2请发送的"));
		dlmFriends.add(0, new FriendInfo("4.jpg", "光和热"));
		dlmFriends.add(0, new FriendInfo("8.jpg", "Terre"));
		dlmFriends.add(0, new FriendInfo("3.jpg", "机房大概"));
		dlmFriends.add(0, new FriendInfo("dzq4.jpg", "bxc"));
		dlmFriends.add(0, new FriendInfo("dzq5.jpg", "人员"));
	}

	private void setFriendsPanel() {
		panel_friends = new FriendsPanel();
		panel_friends.setBackground(new Color(54, 63, 72));
		panel_friends.setBounds(80, 40, 220, 550);

		panel_friends.setLayout(null);
		contentPane.add(panel_friends);

		searchTextField = new JTextField();
		stf = new SearchTextField();
		searchTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				searchTextField.setText("");
				stf.isFocusGained(true);
				searchTextField.setForeground(Color.BLACK);
			}

			@Override
			public void focusLost(FocusEvent e) {
				searchTextField.setText("搜索");
				stf.isFocusGained(false);
				searchTextField.setForeground(Color.WHITE);
			}

		});
		searchTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		searchTextField.setHorizontalAlignment(SwingConstants.CENTER);
		searchTextField.setText("搜索");
		searchTextField.setBounds(20, 10, 180, 28);
		searchTextField.setForeground(Color.WHITE);
		searchTextField.setColumns(1);
		searchTextField.setUI(stf);
		panel_friends.add(searchTextField);

		scrollPane = new JScrollPane();
		scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		scrollPane.setBackground(new Color(0, 0, 0, 0));
		scrollPane.setBounds(0, 48, 220, 502);
		panel_friends.add(scrollPane);

		getData();
		friendsList = new JList<Object>();
		friendsList.setBorder(new EmptyBorder(0, 0, 0, 0));
		scrollPane.setViewportView(friendsList);
		scrollPane.getVerticalScrollBar().setUI(
				new MyScrollBarUI(new Color(54, 63, 72)));
		friendsList.setBackground(new Color(54, 63, 72));
		friendsList.setCellRenderer(new FriendItem());
		friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		friendsList.setModel(dlmFriends);

		friendsList.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {

				// 判断是否连续点击同一个，如果是同一个则不做处理
				if (friendsList.getSelectedValue() != whichFriendsSelected) {
					textArea.setEditable(true);
					whichFriendsSelected = (FriendInfo) friendsList
							.getSelectedValue();
					friendName.setText(whichFriendsSelected.getName());
					if (friendsList.getSelectedIndex() % 2 == 0) {
						isOnlineLabel.setText("online");
						onlineImg.setIcon(new ImageIcon(
								"resources/img/online.png"));
					} else {
						isOnlineLabel.setText("offline");
						onlineImg.setIcon(new ImageIcon(
								"resources/img/offline.png"));
					}

				}
			}
		});
	}

	private void setChat() {

		panel_chat = new ChatPanel();
		panel_chat.setBackground(new Color(236, 236, 236));
		panel_chat.setBounds(300, 40, 520, 550);
		panel_chat.setLayout(null);
		contentPane.add(panel_chat);

		topPanel = new JPanel();
		// topPanel.setOpaque(false);
		topPanel.setBackground(new Color(0, 0, 0, 0));
		topPanel.setBounds(0, 0, 520, 45);
		topPanel.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				setLocation(jfX + (e.getXOnScreen() - nowX),
						jfY + (e.getYOnScreen() - nowY));
			}
		});
		topPanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				nowX = e.getXOnScreen();
				nowY = e.getYOnScreen();
				jfX = getX();
				jfY = getY();
			}
		});

		panel_chat.add(topPanel);
		topPanel.setLayout(null);
		minButton = new JButton("一");
		minButton.setBounds(460, 0, 30, 25);
		topPanel.add(minButton);
		minButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setExtendedState(JFrame.ICONIFIED);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				minButton.setBackground(new Color(120, 175, 250));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				minButton.setBackground(new Color(236, 236, 236));
			}
		});
		minButton.setMargin(new Insets(0, 0, 0, 0));
		minButton.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 12));
		minButton.setUI(new OvalButton(new Color(70, 145, 250), false));
		minButton.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 12));

		exitButton = new JButton("X");
		exitButton.setBounds(490, 0, 30, 25);
		topPanel.add(exitButton);
		exitButton.setMargin(new Insets(0, 0, 0, 0));
		exitButton.setUI(new OvalButton(Color.RED, true));

		exitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				exitButton.setBackground(new Color(250, 100, 110));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				exitButton.setBackground(new Color(236, 236, 236));
			}
		});

		friendName = new JTextField("");
		friendName.setFocusable(false);
		friendName.setEditable(false);
		friendName.setBorder(null);
		friendName.setFont(new Font("宋体", Font.BOLD, 16));
		friendName.setBounds(27, 0, 321, 25);
		topPanel.add(friendName);

		onlineImg = new JLabel("");
		onlineImg.setBounds(27, 20, 10, 25);
		// onlineImg.setIcon(new ImageIcon("resources/img/offline.png"));

		isOnlineLabel = new JTextField("");
		isOnlineLabel.setFocusable(false);
		isOnlineLabel.setEditable(false);
		isOnlineLabel.setBorder(null);
		isOnlineLabel.setForeground(Color.GRAY);
		isOnlineLabel.setBounds(37, 20, 143, 25);
		isOnlineLabel.setForeground(new Color(201, 199, 199));
		// lblNewLabel.setIcon(new ImageIcon("resources/img/offline.png"));
		topPanel.add(onlineImg);
		topPanel.add(isOnlineLabel);

		friendName.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				setLocation(jfX + (e.getXOnScreen() - nowX),
						jfY + (e.getYOnScreen() - nowY));
			}
		});
		friendName.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				nowX = e.getXOnScreen();
				nowY = e.getYOnScreen();
				jfX = getX();
				jfY = getY();
			}
		});

		chat_scrollPane = new JScrollPane();
		chat_scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		chat_scrollPane.setBackground(new Color(0, 0, 0, 0));
		chat_scrollPane.setBounds(0, 46, 520, 404);
		panel_chat.add(chat_scrollPane);

		// chatMessageList = new JList<Object>();
		// chatMessageList.setBorder(new EmptyBorder(0, 0, 0, 0));
		// chat_scrollPane.setViewportView(chatMessageList);
		// chat_scrollPane.getVerticalScrollBar().setUI(
		// new MyScrollBarUI(new Color(236, 236, 236)));
		// chatMessageList.setBackground(new Color(236, 236, 236));
		// chatMessageList.setCellRenderer(new FriendItem());
		// chatMessageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//
		// chatMessageList.setModel(dlmFriends);

		edit_scrollPane = new JScrollPane();
		edit_scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		edit_scrollPane.setBackground(new Color(0, 0, 0, 0));
		edit_scrollPane.setBounds(0, 451, 520, 73);
		panel_chat.add(edit_scrollPane);

		textArea = new JTextArea();
		textArea.setBackground(new Color(236, 236, 236));
		textArea.setEditable(false);
		textArea.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				if (whichFriendsSelected != null) {
					textArea.setBackground(Color.WHITE);
					panel.setBackground(Color.WHITE);
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				textArea.setBackground(new Color(236, 236, 236));
				panel.setBackground(new Color(236, 236, 236));
			}
		});
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		textArea.setFont(new Font("仿宋", Font.BOLD, 14));
		textArea.setMargin(new Insets(5, 5, 5, 5));
		edit_scrollPane.setViewportView(textArea);

		panel = new JPanel();
		panel.setBorder(new EmptyBorder(0, 0, 0, 0));
		panel.setBackground(new Color(236, 236, 236));
		panel.setBounds(0, 525, 520, 25);
		panel_chat.add(panel);
		panel.setLayout(null);

		JButton btnNewButton = new JButton("发送");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (whichFriendsSelected != null) {
					String text = textArea.getText();
					textArea.setText("");
					FontMetrics metrics;
					metrics = textArea.getFontMetrics(textArea.getFont());
					int textH = metrics.getHeight();
					int textW = metrics.stringWidth(text);
					// friendName.setText(text);
					System.out.println(textH + "--" + textW);
				}
			}
		});
		btnNewButton.setBounds(455, 2, 60, 21);
		panel.add(btnNewButton);
	}
}
